package edu.iupui.csci.mortgageplanner;

import edu.iupui.csci.mortgageplanner.util.Mortgage;
import edu.iupui.csci.mortgageplanner.util.MortgagePeriodDetails;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class MonthDetailActivity extends Activity implements OnClickListener{
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_month_detail);
		// Show the Up button in the action bar.
		setupActionBar();
		
		
		Long yearId = getIntent().getLongExtra("yearId",0);
		Long monthId = getIntent().getLongExtra("monthId", 0);
		
		Long periodId= 0l;
		
		try{
			periodId = (yearId -1)* 12 + monthId -1;
			
			Mortgage m = Mortgage.getMortgage();
			MortgagePeriodDetails periodInfo = m.getMortgageDetails().get(periodId.intValue());
			
			TextView title = (TextView) findViewById(R.id.month_detail_title_label_id);
			title.setText("Details for " + getIntent().getStringExtra("title"));
			
			TextView principal = (TextView) findViewById(R.id.month_detail_principal_value_id);
			principal.setText(String.format("%1$,.2f", periodInfo.getPrincipal()));
			
			TextView interest = (TextView) findViewById(R.id.month_detail_interest_value_id);
			interest.setText(String.format("%1$,.2f", periodInfo.getInterest()));
			
			TextView pmi = (TextView) findViewById(R.id.month_detail_pmi_value_id);
			pmi.setText(String.format("%1$,.2f", periodInfo.getPmi()));
			
			TextView taxes = (TextView) findViewById(R.id.month_detail_taxes_value_id);
			taxes.setText(String.format("%1$,.2f", periodInfo.getTaxes()));
			
			TextView insurance = (TextView) findViewById(R.id.month_detail_home_insurance_value_id);
			insurance.setText(String.format("%1$,.2f", periodInfo.getInsurance()));
			
			TextView totalInterest = (TextView) findViewById(R.id.month_detail_total_interest_value_id);
			totalInterest.setText(String.format("%1$,.2f", periodInfo.getTotalInterest()));
			
			TextView remainingPrincipal = (TextView) findViewById(R.id.month_detail_remaining_principal_value_id);
			remainingPrincipal.setText(String.format("%1$,.2f", periodInfo.getRemainingPrincipal()));
			
			TextView totalPrincipal = (TextView) findViewById(R.id.month_detail_total_principal_value_id);
			totalPrincipal.setText(String.format("%1$,.2f", periodInfo.getTotalPrincipal()));
			
			EditText additionalPayment = (EditText) findViewById(R.id.month_detail_additional_payment_value_id);
			additionalPayment.setText(String.format("%1$,.2f", periodInfo.getAdditionalPayment()));
			
			Button saveButton = (Button) findViewById(R.id.month_detail_save_button_id);
			saveButton.setOnClickListener(this);
			
		}catch(Exception e) {
			e.printStackTrace();
		}

	}

	/**
	 * Set up the {@link android.app.ActionBar}.
	 */
	private void setupActionBar() {
		try{
			getActionBar().setDisplayHomeAsUpEnabled(true);
		}catch (Throwable e){
			
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.month_detail, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			//NavUtils.navigateUpFromSameTask(this);
			finish();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onClick(View arg0) {
		try{
			EditText additionalPayment = (EditText) findViewById(R.id.month_detail_additional_payment_value_id);
			double addPayment = Double.parseDouble(additionalPayment.getText().toString());
			
			Long yearId = getIntent().getLongExtra("yearId",0);
			Long monthId = getIntent().getLongExtra("monthId", 0);
			
			Long periodId = (yearId -1)* 12 + monthId -1;
			Mortgage.getMortgage().recalculateMortgageDetails(periodId.intValue(), addPayment);
			
    		String[] months = new String[]{"JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"};
	    	Intent intent = new Intent(getApplicationContext(),MonthDetailActivity.class);
			intent.putExtra("yearId", yearId);
			intent.putExtra("monthId", monthId);
			intent.putExtra("title",months[new Long(monthId -1).intValue()] + ", " + (2013 + yearId -1));
			startActivity(intent);
			finish();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


}
